package com.zrc.superframe_base.mvx.mvc

import android.os.Bundle
import androidx.annotation.LayoutRes
import com.zrc.superframe_base.mvx.base.BaseActivity
import com.zrc.superframe_net.interfac.NetBase
import io.reactivex.disposables.Disposable

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Describe:
 */
abstract class BaseMvcActivity: BaseActivity() , NetBase {
    //请求队列（界面销毁时需要回收的）
    private var requestQueue= ArrayList<Disposable>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getLayoutId())
        initView()
        initListener()
        initData()
    }

    @LayoutRes
    abstract fun getLayoutId(): Int

    open fun initView(){}

    open fun initListener(){}

    open fun initData(){}

    override fun onDestroy() {
        super.onDestroy()
        netGc()
    }

    override fun addRequestQueue(d: Disposable)=if (requestQueue .contains(d)) {
        requestQueue.remove(d)
        false
    }else{
        requestQueue.add(d)
        true
    }

    override fun removeRequestQueue(d: Disposable) {
        requestQueue.remove(d)
    }

    override fun netGc() {
        requestQueue.forEach { //取消队列
            if (!it.isDisposed) {
                it.dispose()
            }
        }
        requestQueue.clear()
    }
}
